
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <title>浏览页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        <link rel="stylesheet" href="../../demos/css/console2.css" />
        <style>

            .pear-card2 .title {
                color: gray;
                font-size: 16px;
                margin-top: 14px;
            }
            .pear-this, .pear-text {
                color: #2d8cf0 !important;
                font-size: 18px;
            }
            .layui-btn-primary {
                color: #000;
                font-size: 14px;
                border-color: rgb(210, 210, 210);
                background: 0px 0px;
            }
        </style>
    </head>
    <body class="pear-container">
    
        <!-- 顶部查询表单 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form top-search-from">
                    <div class="layui-form-item">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-block">
                            <div class="layui-btn-group ">
                                <a class="layui-btn layui-btn-normal layui-btn-1"  onclick="type_select(1);" style="width: 80px;">客户端</a>
                                <a class="layui-btn layui-btn-primary layui-btn-2" onclick="type_select(2);" style="width: 80px;">后台</a>
                                <a class="layui-btn layui-btn-primary layui-btn-3" onclick="type_select(3);" style="width: 80px;">脚本</a>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <div name="status" id="status" value="" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">来源</label>
                        <div class="layui-input-block">
                            <div name="source" id="source" value="" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">指令模板</label>
                        <div class="layui-input-block">
                            <div name="command_template_id" id="command_template_id" value="" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">片段ID</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block">
                                <input type="text" autocomplete="off" name="partid" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">时长(秒)</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block">
                                <input type="text" autocomplete="off" name="tts[]" class="layui-input inline-block">
                                -
                                <input type="text" autocomplete="off" name="tts[]" class="layui-input inline-block">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">执行时间</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block" id="exec_time">
                                <input type="text" autocomplete="off" name="created_at[]" id="exec_time-date-start" class="layui-input inline-block" placeholder="开始时间">
                                -
                                <input type="text" autocomplete="off" name="created_at[]" id="exec_time-date-end" class="layui-input inline-block" placeholder="结束时间">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">完成时间</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block" id="finish_at">
                                <input type="text" autocomplete="off" name="created_at[]" id="finish_at-date-start" class="layui-input inline-block" placeholder="开始时间">
                                -
                                <input type="text" autocomplete="off" name="created_at[]" id="finish_at-date-end" class="layui-input inline-block" placeholder="结束时间">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">添加时间</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block" id="created_at">
                                <input type="text" autocomplete="off" name="created_at[]" id="created_at-date-start" class="layui-input inline-block" placeholder="开始时间">
                                -
                                <input type="text" autocomplete="off" name="created_at[]" id="created_at-date-end" class="layui-input inline-block" placeholder="结束时间">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label"></label>
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                    <div class="toggle-btn">
                        <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                        <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
                    </div>
                </form>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-header">
                数据统计
            </div>
            <div class="layui-card-body">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md2 layui-col-sm6 layui-col-xs6">
                        <div class="pear-card2">
                            <div class="title">排队中数</div>
                            <div class="status_0_nums pear-text">0</div>
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm6 layui-col-xs6">
                        <div class="pear-card2">
                            <div class="title">失败数</div>
                            <div class="status_3_nums pear-text">0</div>
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm6 layui-col-xs6">
                        <div class="pear-card2">
                            <div class="title">完成数</div>
                            <div class="status_2_nums pear-text">0</div>
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm6 layui-col-xs6">
                        <div class="pear-card2">
                            <div class="title">超过30秒数</div>
                            <div class="greater_than_30_seconds_nums pear-text">0</div>
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm6 layui-col-xs6">
                        <div class="pear-card2">
                            <div class="title">近30分钟失败数</div>
                            <div class="nearly_30_minutes_fail_nums pear-text">0</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 数据表格 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <!-- 表格顶部工具栏 -->
        <!--<script type="text/html" id="table-toolbar">-->
            <!--<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" permission="app.admin.font.index.insert">-->
                <!--<i class="layui-icon layui-icon-add-1"></i>新增-->
            <!--</button>-->
            <!--<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" permission="app.admin.font.index.delete">-->
                <!--<i class="layui-icon layui-icon-delete"></i>删除-->
            <!--</button>-->
        <!--</script>-->

        <!-- 表格行工具栏 -->
        <!--<script type="text/html" id="table-bar">-->
            <!--<button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="app.admin.font.index.update">编辑</button>-->
            <!--<button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="app.admin.font.index.delete">删除</button>-->
        <!--</script>-->

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/common.js"></script>
        <script src="/app/admin/admin/js/functions.js"></script>
        <script src="/app/admin/admin/js/md5.js"></script>
        
        <script>
            let use_type = 'user';

            // 相关常量
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/queue/index/select";

            let where = {};

            type_select(1)

            // 字段 添加时间 exec_time
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#exec_time",
                    range: ["#exec_time-date-start", "#exec_time-date-end"],
                    type: "datetime",
                    mark: date_today_mark,
                });
            })

            // 字段 添加时间 finish_at
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#finish_at",
                    range: ["#finish_at-date-start", "#finish_at-date-end"],
                    type: "datetime",
                    mark: date_today_mark,
                });
            })
            // 字段 添加时间 created_at
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#created_at",
                    range: ["#created_at-date-start", "#created_at-date-end"],
                    type: "datetime",
                    mark: date_today_mark,
                });
            })

            // 字段 状态 status
            layui.use(["jquery", "xmSelect"], function() {
                layui.xmSelect.render({
                    el: "#status",
                    name: "status",
                    filterable: true,
                    data: [{"value":"","name":"全部"},{"value":"0","name":"排队中"},{"value":"1","name":"生成中"},{"value":"2","name":"已完成"},{"value":"3","name":"已失败"}],
                    initValue: ["0"],
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true
                })
            });

            // 字段 指令模板 command_template_id
            layui.use(["jquery", "xmSelect", "popup"], function() {
                layui.$.ajax({
                    url: "/app/admin/image/aiimagecommand/select?format=select&limit=1000",
                    dataType: "json",
                    async:true,
                    headers: get_header({"format":"select"}),
                    success: function (res) {
                        layui.xmSelect.render({
                            el: "#command_template_id",
                            name: "command_template_id",
                            filterable: true,
                            data: res.data,
                            initValue: [],
                            model: {"icon":"hidden","label":{"type":"text"}},
                            clickClose: true,
                            radio: true,
                        })
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }
                    }
                });
            });

            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util"], function() {
                let table = layui.table;
                let form = layui.form;
                $ = layui.$;
                let common = layui.common;
                let util = layui.util;
                


                // 表格顶部工具栏事件
                table.on("toolbar(data-table)", function(obj) {
                    if (obj.event === "refresh") {
                        refreshTable();
                    }
                });

                // 表格顶部搜索事件
                form.on("submit(table-query)", function(data) {
                    where = searchWhere(data.field);
                    where.use_type = use_type;
                    table.reload("data-table", {
                        page: {
                            curr: 1
                        },
                        where: where,
                        headers: get_header(where)
                    })
                    return false;
                });
                
                // 表格顶部搜索重置事件
                form.on("submit(table-reset)", function(data) {
                    where = {};
                    where.use_type = use_type;
                    table.reload("data-table", {
                        where: where,
                        headers: get_header(where)
                    })
                });

                // 表格排序事件
                table.on("sort(data-table)", function(obj){
                    where.field=obj.field;
                    where.order=obj.type;
                    where.use_type = use_type;
                    table.reload("data-table", {
                        initSort: obj,
                        scrollPos: "fixed",
                        where: where,
                        headers: get_header(where)
                    });
                });

                // 刷新表格数据
                window.refreshTable = function(param) {
                    table.reloadData("data-table", {
                        scrollPos: "fixed"
                    });
                }

                window.view = function (param) {
                    let content = param ? uncompile(param) : '';

                    layer.open({
                        type: 1,
                        title: "预览",
                        shade: 0.1,
                        maxmin: true,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"900px", common.isModile()?"100%":"520px"],
                        content: '<div style="margin: 20px;font-size: 16px;letter-spacing: 2px;">  <textarea disabled rows="23" style="height: 100%;width: 100%">'+content+'</textarea></div>'
                    });
                }
            })

            function table_render() {
                layui.use(["table", "form", "common", "popup", "util", 'tab', ], function() {
                    let table = layui.table;
                    let form = layui.form;
                    let $ = layui.$;
                    let util = layui.util;

                    let ishide = true;
                    if(use_type=='user'){
                        ishide = false;
                    }

                    // 表头参数
                    let cols = [
                        {
                            type: "checkbox", align: "center"
                        },{
                            title: "ID",align: "center", field: "id",width:100,sort:true,
                        },{
                            title: "用户编号",align: "center", field: "user_sn",width:120,hide:ishide
                        },{
                            title: "方案名称",align: "center", field: "plan_name",width:150,
                        },{
                            title: "指令模板",align: "center", field: "command_template_name",width:150,
                        },{
                            title: "片段ID",align: "center", field: "partid",width:120,sort:true,templet: function (d) {
                                if(d.type=='user'){
                                    return '作品ID '+d.partid;
                                }else if(d.type=='admin'){
                                    return '分段ID '+d.partid;
                                }else if(d.type=='cron'){
                                    return '分段ID '+d.partid;
                                }
                            }
                        },{
                            title: "发起来源",align: "center", field: "source_name",width:180,sort:true,
                        },{
                            title: "请求指令",align: "center", field: "param",width:220,templet: function (d) {
                                // 预览数据
                                return '<a href="javascript:;" onclick="view(\''+compile(d.param)+'\')">预览</a> '+d.param;
                            }
                        },{
                            title: "响应结果",align: "center", field: "result",width:220, templet: function (d) {
                                // 预览数据
                                return '<a href="javascript:;" onclick="view(\''+compile(d.result)+'\')">预览</a> '+d.result;
                            }
                        },{
                            title: "图片1",align: "center", field: "images1",width:100,templet: function (d) {
                                if(d['images1']){
                                    return '<img src="'+encodeURI(d['images1'])+'" style="max-width:32px;max-height:32px;" alt="" />';
                                }else{
                                    return '';
                                }
                            }
                        },{
                            title: "图片2",align: "center", field: "images2",width:100,templet: function (d) {
                                if(d['images2']){
                                    return '<img src="'+encodeURI(d['images2'])+'" style="max-width:32px;max-height:32px;" alt="" />';
                                }else{
                                    return '';
                                }
                            }
                        },{
                            title: "状态",align: "center", field: "status",minWidth:200, templet: function (d) {
                                if(d.status==0){
                                    return '<span style="color: #c50ecf">'+d.queue_message+'</span>';
                                }else if(d.status==1){
                                    return '<span style="color: #13b162">生成中</span>';
                                }else if(d.status==2){
                                    return  '<span style="color: #234be3">已完成</span>';
                                }else if(d.status==3 || d.status==4){
                                    return  '<span style="color: ">已失败</span>';
                                }
                            }
                        },{
                            title: "服务器",align: "center", field: "ip",width: 150,
                        },{
                            title: "执行时长(秒)",align: "center", field: "tts",width: 120,
                        },{
                            title: "执行时间",align: "center", field: "start_time",width: 180,
                        },{
                            title: "结束时间",align: "center", field: "end_time",width: 180,
                        },{
                            title: "请求ID",align: "center", field: "requestid",width: 180,
                        },{
                            title: "添加时间",align: "center", field: "created_at",width: 180,
                        }
                    ];

                    where.use_type = use_type;
                    // 渲染表格
                    table.render({
                        elem: "#data-table",
                        url: SELECT_API,
                        page: true,
                        limits: [10, 20, 50, 100, 500],
                        limit: 20,
                        where: where,
                        headers: get_header(where),
                        cols: [cols],
                        skin: "line",
                        size: "lg",
                        toolbar: "#table-toolbar",
                        autoSort: false,
                        defaultToolbar: [{
                            title: "刷新",
                            layEvent: "refresh",
                            icon: "layui-icon-refresh",
                        }, "filter", "print", "exports"],
                        done: function (res) {
                            layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});
                            var data  = res.total_data;
                            $.each(data,function (i,v) {
                                $('.'+i).text(v);
                            })
                        }
                    });
                })
            }
            function type_select(type) {
                layui.$('.layui-btn-'+type).removeClass("layui-btn-primary").addClass('layui-btn-normal');
                layui.$('.layui-btn-'+type).siblings('a').addClass("layui-btn-primary").removeClass('layui-btn-normal');
                if(type==1){
                    use_type = 'user'
                    // 字段 状态 status
                    layui.use(["jquery", "xmSelect"], function() {
                        layui.xmSelect.render({
                            el: "#source",
                            name: "source",
                            filterable: true,
                            initValue: [],
                            data: [
                                {"value":"user_plan_part_image_view_batch","name":"用户批量生图"},
                                {"value":"user_plan_part_image_view_one","name":"用户重新生图"},
                                {"value":"user_plan_part_image_save","name":"用户生成作品"},
                            ],
                            model: {"icon":"hidden","label":{"type":"text"}},
                            clickClose: true,
                            radio: true
                        })
                    });
                }else if(type==2){
                    use_type = 'admin'
                    // 字段 状态 status
                    layui.use(["jquery", "xmSelect"], function() {
                        layui.xmSelect.render({
                            el: "#source",
                            name: "source",
                            filterable: true,
                            initValue: [],
                            data: [
                                {"value":"admin_plan_part_image_edit","name":"后台片段生图"},
                                {"value":"admin_batch_image_test","name":"后台AI生图调试"},
                                {"value":"admin_plan_role_image_view","name":"后台角色生图预览"},
                                {"value":"admin_plan_part_view_image","name":"后台AI分段生图"},
                                {"value":"admin_plan_part_image_view_image","name":"后台AI分段图生图"},
                            ],
                            model: {"icon":"hidden","label":{"type":"text"}},
                            clickClose: true,
                            radio: true
                        })
                    });
                }else if(type==3){
                    use_type = 'cron'
                    // 字段 状态 status
                    layui.use(["jquery", "xmSelect"], function() {
                        layui.xmSelect.render({
                            el: "#source",
                            name: "source",
                            filterable: true,
                            initValue: [],
                            data: [
                                {"value":"cron_plan_part_image_save","name":"脚本分段预生图"},
                            ],
                            model: {"icon":"hidden","label":{"type":"text"}},
                            clickClose: true,
                            radio: true
                        })
                    });
                }
                // 表格渲染
                table_render();
            }
        </script>
    </body>
</html>
